package main

import (
	"fmt"
	"net/smtp"
	"strings"
)

func sendToMail(user, password, host, to, subject, body string) error {
	hp := strings.Split(host, ":")
	auth := smtp.PlainAuth("", user, password, hp[0])

	str := strings.Replace("From: "+user+"~To: "+to+"~Subject: "+subject+"~~", "~", "\r\n", -1) + body
	sendTo := strings.Split(to, ";")
	err := smtp.SendMail(host, auth, user, sendTo, []byte(str))
	return err
}

func main() {
	user := "qieshinanming@163.com"
	password := "noandnosay"
	host := "smtp.163.com:25"
	to := "boaitianxia@163.com"

	subject := "使用Golang发送邮件"

	body := `aaaa`
	fmt.Println("send email")
	err := sendToMail(user, password, host, to, subject, body)
	if err != nil {
		fmt.Println("Send mail error!")
		fmt.Println(err)
	} else {
		fmt.Println("Send mail success!")
	}
}
